﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;
namespace DAO
{
    public class SachDAO
    {
        public DataTable LayTable()
        {
            String sql = "SELECT s.MaSach , TenSach, s.MaTheLoai ,SoLuong ,s.MaTG ,DonGia ,TenTacGia,TenTheLoai FROM Sach s,TacGia tg,TheLoai tl where s.MaTheLoai = tl.MaTheLoai  AND s.MaTG = tg.MaTG ";
            SqlConnection con = DataProvider.ConnectDB();

            SqlCommand command = new SqlCommand(sql, con);
            con.Open();
            SqlDataAdapter adapter = new SqlDataAdapter(command);

            DataTable dt = new DataTable();
            adapter.Fill(dt);

            con.Close();
            return dt;
        }
        public DataTable LayTableTheoTacGia(int tg)
        {
            String sql = "SELECT s.MaSach , TenSach, s.MaTheLoai ,SoLuong ,s.MaTG ,DonGia ,TenTacGia,TenTheLoai FROM Sach s,TacGia tg,TheLoai tl where s.MaTheLoai = tl.MaTheLoai  AND s.MaTG = tg.MaTG and s.MaTG = " + tg;
            SqlConnection con = DataProvider.ConnectDB();
            con.Open();
            SqlCommand command = new SqlCommand(sql, con);

            SqlDataAdapter adapter = new SqlDataAdapter(command);

            DataTable dt = new DataTable();
            adapter.Fill(dt);

            con.Close();
            return dt;
        }
        public DataTable LayTableTheoLoai(int tl)
        {
            String sql = "SELECT s.MaSach , TenSach, s.MaTheLoai ,SoLuong ,s.MaTG ,DonGia ,TenTacGia,TenTheLoai FROM Sach s,TacGia tg,TheLoai tl where s.MaTheLoai = tl.MaTheLoai  AND s.MaTG = tg.MaTG AND  s.MaTheLoai = " + tl;
            SqlConnection con = DataProvider.ConnectDB();
            con.Open();
            SqlCommand command = new SqlCommand(sql, con);

            SqlDataAdapter adapter = new SqlDataAdapter(command);

            DataTable dt = new DataTable();
            adapter.Fill(dt);

            con.Close();
            return dt;
        }
        public DataTable LayTableTheoLoaiVaTG(int tl, int tg)
       
        {
            String sql = "SELECT s.MaSach , TenSach, s.MaTheLoai ,SoLuong ,s.MaTG ,DonGia ,TenTacGia,TenTheLoai FROM Sach s,TacGia tg,TheLoai tl where s.MaTheLoai = tl.MaTheLoai  AND s.MaTG = tg.MaTG AND s.MaTheLoai = " + tl+ "and s.MaTG = " +tg;
            SqlConnection con = DataProvider.ConnectDB();
            con.Open();
            SqlCommand command = new SqlCommand(sql, con);

            SqlDataAdapter adapter = new SqlDataAdapter(command);

            DataTable dt = new DataTable();
            adapter.Fill(dt);

            con.Close();
            return dt;
        }
        public void them(SachDTO sach)
        {
            String sql = "Insert into Sach(TenSach,MaTheLoai,SoLuong,MaTG,DonGia) values(N'" + sach.Tensach + "'," + sach.Matheloai + "," + sach.Soluong + "," + sach.Matacgia + "," + sach.DonGia + ")";
            SqlConnection con = DataProvider.ConnectDB();
            con.Open();
            SqlCommand command = new SqlCommand(sql, con);
            int res = -1;
            res = command.ExecuteNonQuery();
            con.Close();
        }

        public void sua(SachDTO sach)
        {
            String sql = "UPDATE Sach SET TenSach =N'" + sach.Tensach + "' ,MaTheLoai=" + sach.Matheloai + ",SoLuong=" + sach.Soluong + ",MaTG=" + sach.Matacgia +",DonGia = " +sach.DonGia+ " where MaSach =" + sach.Masach + "";
            SqlConnection con = DataProvider.ConnectDB();
            con.Open();
            SqlCommand command = new SqlCommand(sql, con);
            int res = -1;
            res = command.ExecuteNonQuery();
            con.Close();
        }
        public void suaSoLuong(int sl, int ma)
        {
            String sql = "UPDATE Sach SET SoLuong=" + sl + " where MaSach =" + ma + "";
            SqlConnection con = DataProvider.ConnectDB();
            con.Open();
            SqlCommand command = new SqlCommand(sql, con);
            int res = -1;
            res = command.ExecuteNonQuery();
            con.Close();
        }
        public void Xoa(SachDTO sach)
        {
            String sql = "DELETE from Sach  where MaSach =" + sach.Masach ;
            SqlConnection con = DataProvider.ConnectDB();
            con.Open();
            SqlCommand command = new SqlCommand(sql, con);
            int res = -1;
            res = command.ExecuteNonQuery();
            con.Close();
        }
        
    }
}
